/*
 * #%L
 * SwiftUICommonUtilities
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.utils;

import java.beans.ExceptionListener;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.linkwithweb.products.swiftui.context.DBUpdateStatement;

/**
 * @author ashwin kumar
 * 
 */
public class ObjectSerializer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DBUpdateStatement dbUpdateStatement = new DBUpdateStatement();
		dbUpdateStatement.setUpdateStatement("select * from tab");
		List arguments = new ArrayList();
		arguments.add("test");
		arguments.add(new Date());
		arguments.add(new Double(1));
		dbUpdateStatement.setArguments(arguments);

		ByteArrayOutputStream bos = new ByteArrayOutputStream();

		XMLEncoder e = new XMLEncoder(bos);
		e.setExceptionListener(new ExceptionListener() {
			public void exceptionThrown(Exception exception) {
				//exception.printStackTrace();
			}
		});
		e.writeObject(dbUpdateStatement);
		e.close();

		String outputString = bos.toString();
		System.out.println(outputString.replaceAll("\n", "").replaceAll("\r", ""));

		ByteArrayInputStream bis = new ByteArrayInputStream(outputString.replaceAll("\n", "").replaceAll("\r", "")
				.getBytes());
		XMLDecoder decoder = new XMLDecoder(bis);
		DBUpdateStatement dbUpdateStatement1 = (DBUpdateStatement) decoder.readObject();
		System.out.println(dbUpdateStatement1.getUpdateStatement());
	}

	/**
	 * @param obj
	 * @return
	 */
	public static String convertObjectToString(Object obj) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		XMLEncoder e = new XMLEncoder(bos);
		e.setExceptionListener(new ExceptionListener() {
			public void exceptionThrown(Exception exception) {
				//exception.printStackTrace();
			}
		});
		e.writeObject(obj);
		e.close();

		String outputString = bos.toString();
		return outputString;
	}

	/**
	 * @param stringToConvert
	 * @return
	 */
	public static Object convertStringToObject(String stringToConvert) {
		ByteArrayInputStream bis = new ByteArrayInputStream(stringToConvert
				.getBytes());
		XMLDecoder decoder = new XMLDecoder(bis);
		return decoder.readObject();
	}

}
